package com.punai.externalccb.service.impl;

import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import com.punai.externalccb.bo.EcoCbfeeitemBo;
import com.punai.externalccb.domain.EcoBidnode;
import com.punai.externalccb.domain.EcoCbfeeitem;
import com.punai.externalccb.domain.EcoCbsummary;
import com.punai.externalccb.mapper.EcoBidnodeMapper;
import com.punai.externalccb.mapper.EcoBqitemmateMapper;
import com.punai.externalccb.mapper.EcoCbfeeitemMapper;
import com.punai.externalccb.mapper.EcoCbsummaryMapper;
import com.punai.externalccb.service.IEcoCbfeeitemService;
import com.punai.prodatalibrary.constants.ProjectLibConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * cbfeeitemService业务层处理
 *
 * @author tj
 * @date 2023-04-11
 */
@Service
public class EcoCbfeeitemServiceImpl extends ServiceImpl<EcoCbfeeitemMapper, EcoCbfeeitem> implements IEcoCbfeeitemService
{
    @Autowired
    private EcoCbfeeitemMapper ecoCbfeeitemMapper;
    @Autowired
    private EcoBqitemmateMapper ecoBqitemmateMapper;
    @Autowired
    private EcoBidnodeMapper ecoBidnodeMapper;
    @Autowired
    private EcoCbsummaryMapper ecoCbsummaryMapper;

    /**
     * 查询cbfeeitem
     *
     * @param id cbfeeitem主键
     * @return cbfeeitem
     */
    @Override
    public EcoCbfeeitem selectEcoCbfeeitemById(String id)
    {
        return ecoCbfeeitemMapper.selectEcoCbfeeitemById(id);
    }

    /**
     * 查询cbfeeitem列表
     *
     * @param ecoCbfeeitem cbfeeitem
     * @return cbfeeitem
     */
    @Override
    public List<EcoCbfeeitemBo> selectEcoCbfeeitemList(EcoCbfeeitem ecoCbfeeitem)
    {
        ecoCbfeeitem.setBidnodeid(1);
        List<EcoCbfeeitem> cbFeeItems = ecoCbfeeitemMapper.selectEcoCbfeeitemList(ecoCbfeeitem);
        ListValueMap<Integer,EcoCbfeeitem> cbFeeItemListValueMap = new ListValueMap<>();
        for (EcoCbfeeitem cbFeeItem : cbFeeItems) {
            cbFeeItemListValueMap.putValue(cbFeeItem.getPid(),cbFeeItem);
        }
        List<EcoCbfeeitemBo> result = new ArrayList<>();
        List<EcoCbfeeitem> feeItemList = cbFeeItemListValueMap.get(0);
        if (CollUtil.isNotEmpty(feeItemList)){
            for (int i = 0; i < feeItemList.size(); i++) {
                EcoCbfeeitemBo convert = EcoCbfeeitemBo.convert(feeItemList.get(i));
                Integer id = feeItemList.get(i).getCbfeeitemid();
                result.add(convert);
                setChild(id,cbFeeItemListValueMap,convert);
            }
        }
        return result;
    }

    private void setChild(Integer pid,ListValueMap<Integer,EcoCbfeeitem> valueMap,EcoCbfeeitemBo bo){
        List<EcoCbfeeitem> feeItemList = valueMap.get(pid);
        List<EcoCbfeeitemBo> res = new ArrayList<>();
        if (CollUtil.isNotEmpty(feeItemList)){
            for (int i = 0; i < feeItemList.size(); i++) {
                EcoCbfeeitemBo convert = EcoCbfeeitemBo.convert(feeItemList.get(i));
                Integer id = feeItemList.get(i).getCbfeeitemid();
                res.add(convert);
                setChild(id,valueMap,convert);
            }
            bo.setChildren(res);
        }
    }

    @Override
    public List<EcoCbfeeitemBo> selectJsxmxListTabData(EcoCbfeeitem ecoCbfeeitem) {
        List<EcoBidnode> bidnodes = ecoBidnodeMapper.selectEcoBidnodeList(new EcoBidnode(ecoCbfeeitem.getProId(), ecoCbfeeitem.getStagetype()));
        ListValueMap<Integer,EcoBidnode> bidnodeListValueMap = new ListValueMap<>();
        for (EcoBidnode bidnode : bidnodes) {
            bidnodeListValueMap.putValue(bidnode.getPid(),bidnode);
        }
        List<EcoCbfeeitemBo> result = new ArrayList<>();
        List<EcoBidnode> bidnodeList = bidnodeListValueMap.get(0);
        if (CollUtil.isNotEmpty(bidnodeList)){
            for (EcoBidnode bidnode : bidnodeList) {
                List<Integer> codeList = ProjectLibConstants.Cost_TYPE.getCodeList();
                List<EcoCbfeeitem> ecoCbfeeitems = ecoCbfeeitemMapper.selectEcoCbfeeitemList(new EcoCbfeeitem(codeList,0,bidnode.getOid(),bidnode.getProId(),ecoCbfeeitem.getStagetype()));
                EcoCbfeeitemBo convert = EcoCbfeeitemBo.convertBidnode(bidnode,ecoCbfeeitems);
                result.add(convert);
                Integer id = bidnode.getOid();
                setBidnodeChild(id,bidnodeListValueMap,convert,ecoCbfeeitem.getStagetype());
            }
        }
        return result;
    }

    @Override
    public List<EcoCbsummary> selectCbhzListTabData(EcoCbfeeitem ecoCbfeeitem) {
        List<EcoCbsummary> cbsummaryList = ecoCbsummaryMapper.selectEcoCbsummaryList(new EcoCbsummary(1, ecoCbfeeitem.getProId(), ecoCbfeeitem.getStagetype(),0));
        return cbsummaryList;
    }

    private void setBidnodeChild(Integer pid,ListValueMap<Integer,EcoBidnode> valueMap,EcoCbfeeitemBo bo,String stagetype){
        List<EcoBidnode> bidnodeList = valueMap.get(pid);
        List<EcoCbfeeitemBo> res = new ArrayList<>();
        if (CollUtil.isNotEmpty(bidnodeList)){
            for (EcoBidnode bidnode : bidnodeList) {
                Integer bidnodeId = bidnode.getOid();
                List<Integer> codeList = ProjectLibConstants.Cost_TYPE.getCodeList();
                if (bidnodeId == 2) bidnodeId = 3;//单项汇总单位数据
                List<EcoCbfeeitem> ecoCbfeeitems = ecoCbfeeitemMapper.selectEcoCbfeeitemList(new EcoCbfeeitem(codeList,0,bidnodeId,bidnode.getProId(),stagetype));
                EcoCbfeeitemBo convert = EcoCbfeeitemBo.convertBidnode(bidnode,ecoCbfeeitems);
                Integer id = bidnode.getOid();
                res.add(convert);
                setBidnodeChild(id,valueMap,convert,stagetype);
            }
            bo.setChildren(res);
        }
    }

    /**
     * 新增cbfeeitem
     *
     * @param ecoCbfeeitem cbfeeitem
     * @return 结果
     */
    @Override
    public int insertEcoCbfeeitem(EcoCbfeeitem ecoCbfeeitem)
    {
            return ecoCbfeeitemMapper.insert(ecoCbfeeitem);
    }

    /**
     * 修改cbfeeitem
     *
     * @param ecoCbfeeitem cbfeeitem
     * @return 结果
     */
    @Override
    public int updateEcoCbfeeitem(EcoCbfeeitem ecoCbfeeitem)
    {
        return ecoCbfeeitemMapper.updateById(ecoCbfeeitem);
    }

    /**
     * 批量删除cbfeeitem
     *
     * @param ids 需要删除的cbfeeitem主键
     * @return 结果
     */
    @Override
    public int deleteEcoCbfeeitemByIds(String[] ids)
    {
        return ecoCbfeeitemMapper.deleteEcoCbfeeitemByIds(ids);
    }

    /**
     * 删除cbfeeitem信息
     *
     * @param id cbfeeitem主键
     * @return 结果
     */
    @Override
    public int deleteEcoCbfeeitemById(String id)
    {
        return ecoCbfeeitemMapper.deleteEcoCbfeeitemById(id);
    }
}
